//
//  SearchViewController.m
//  HDYS
//
//  Created by qianfeng on 15-4-23.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchHisCell.h"

#define SPACE 5

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height


@interface SearchViewController () <UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

//搜索历史
@property (nonatomic, strong) NSMutableArray *searchHisArray;

@property (nonatomic, strong) UITableView *searchHisTB;
@property (nonatomic, strong) UIButton *clearBtn;

@property (nonatomic, strong) UISearchBar *searchBar;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    NSArray *array = @[@"qunzi "];
    self.searchHisArray = [NSMutableArray arrayWithArray:array];
    
    [self createUI];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSLog(@"%@",self.searchHisArray);
    [self.searchHisTB reloadData];
    if (self.searchHisArray.count == 0) {
        self.searchHisTB.hidden = YES;
    }
//    self.searchHisTB.tableFooterView = self.searchHisArray?nil:self.clearBtn;
}

-(void)createUI{
    CGFloat btnW = 50.0f;
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(SPACE * 2, 5 *SPACE, SCREEN_WIDTH - SPACE * 3 - btnW, 30)];
    [searchBar becomeFirstResponder];
    searchBar.placeholder = @"搜索宝贝";
    searchBar.delegate = self;
    searchBar.barTintColor = [UIColor whiteColor];
    searchBar.barStyle = UIBarStyleBlack;
    [self.view addSubview:searchBar];
    self.searchBar = searchBar;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(searchBar.frame.origin.x + searchBar.frame.size.width + SPACE, searchBar.frame.origin.y, btnW, searchBar.frame.size.height);
    [backBtn setTitle:@"取消" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    backBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    CGFloat searchTBY = searchBar.frame.origin.y + searchBar.frame.size.height + 10;
    UITableView *searchHisTB = [[UITableView alloc] initWithFrame:CGRectMake(0, searchTBY, SCREEN_WIDTH, SCREEN_HEIGHT - searchTBY) style:UITableViewStylePlain];
    searchHisTB.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    searchHisTB.delegate = self;
    searchHisTB.dataSource = self;
    self.searchHisTB = searchHisTB;
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    clearBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 25);
    [clearBtn setTitle:@"清楚历史记录" forState:UIControlStateNormal];
    clearBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [clearBtn addTarget:self action:@selector(clearBtnClick) forControlEvents:UIControlEventTouchUpInside];
    searchHisTB.tableFooterView = clearBtn;
    self.clearBtn = clearBtn;
    
    [self.view addSubview:searchHisTB];
}

#pragma mark - searchBar协议
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSString *searchText = searchBar.text;
//    NSLog(@"%@",searchText);
    
    [_searchHisArray addObject:searchText];
    NSLog(@"_searchHisArray = %@",_searchHisArray);
}

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}


#pragma mark - TableView协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"%ld",_searchHisArray.count);
    return _searchHisArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"searchHisCell";
    SearchHisCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SearchHisCell" owner:self options:nil] lastObject];
    }
    return cell;
}

#pragma mark - 协议
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}

#pragma mark - 点击清楚按钮
-(void)clearBtnClick{
    [_searchHisArray removeAllObjects];
}


#pragma mark - 返回按钮
-(void)backBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
    self.block(NO);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
